#coding=utf-8
'''
Created on 2012-6-28

@author: zhaojp
'''
from django.shortcuts import render_to_response
from django.template import Template, Context
from django.http import HttpResponse
from django.http import Http404

from datetime import date 

from item.models import Book

#动态网页示范
def latest_books(request):
    book_list = Book.objects.order_by('-pub_date');
    return render_to_response('latest_books.html', {'book_list': book_list});

def find_book(request, bookid):
    try:
        bookid = int(bookid);
        book = Book.objects.get(id=int(bookid));
    except BaseException as e:
        print e;
        raise Http404();
    return render_to_response('book.html', {'book':book});

def test_context_and_template(request):
    t = Template('''
        <html>
            <head>
                <title>{{title}}</title>
            </head>
            <body>
                Name:{{persion.name}}
                </br>
                Brith Day:{{persion.birthday.year}}-{{persion.birthday.month}}-{{persion.birthday.day}}
                </br>
                Sex:{{persion.sex.upper}}
                </br>
                Experience:{{persion.experience.0}}
            </body>
        </html>
    ''');
    
    response = t.render(Context({'title':'Tom\'s resume', 'persion':{'name':'Tom', 'birthday':date(1991, 1, 11), 'sex':'Man', 'experience':['Google', 'Yahoo', 'Facebook']}}));
    
    return HttpResponse(response);
